package com.ruoyi.system.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.system.api.domain.SysSerialMax;
import com.ruoyi.system.service.ISysSerialMaxService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 编号最大值Controller
 *
 * @author ruoyi
 * @date 2023-05-12
 */
@Api(tags = "编号最大值")
@RestController
@RequestMapping("/serial/max")
public class SysSerialMaxController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(SysSerialMaxController.class);

    @Autowired
    private ISysSerialMaxService sysSerialMaxService;

    /**
     * 查询编号最大值列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询编号最大值列表", response = SysSerialMax.class)
    public TableDataInfo list(SysSerialMax sysSerialMax) {
        startPage();
        List<SysSerialMax> list = sysSerialMaxService.selectSysSerialMaxList(sysSerialMax);
        return getDataTable(list);
    }

    /**
     * 导出编号最大值列表
     */
    @Log(title = "导出编号最大值列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出编号最大值列表")
    public void export(HttpServletResponse response, SysSerialMax sysSerialMax) {
        List<SysSerialMax> list = sysSerialMaxService.selectSysSerialMaxList(sysSerialMax);
        ExcelUtil<SysSerialMax> util = new ExcelUtil<SysSerialMax>(SysSerialMax.class);
        util.exportExcel(response, list, "编号最大值数据");
    }

    @GetMapping(value = "/data")
    @ApiOperation(value = "通过参数获取编号值", response = SysSerialMax.class)
    public R<SysSerialMax> getSerialValueByParmas(@RequestParam Map paramMap) {
        SysSerialMax sysSerialMax = sysSerialMaxService.getNextValue(paramMap);
        if(sysSerialMax == null){
            return R.fail("未查询到此编号！");
        }
        return R.ok(sysSerialMax);
    }

    /**
     * 新增编号最大值
     */
    @Log(title = "新增编号最大值", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "新增编号最大值")
    public R<SysSerialMax> add(@RequestBody Map paramMap) {
        try{
            SysSerialMax sysSerialMax = sysSerialMaxService.insertSysSerialMax(paramMap);
            if(sysSerialMax == null){
                return R.fail("未查询到此编号！");
            }
            return R.ok(sysSerialMax);
        }catch (Exception e){
            log.error(paramMap.toString() + ":" + e.getMessage());
            return R.fail(e.toString());
        }
    }

    /**
     * 通过类型新增编号最大值
     */
    @Log(title = "通过类型新增编号最大值", businessType = BusinessType.INSERT)
    @GetMapping("/{typeId}")
    @ApiOperation(value = "通过类型新增编号最大值")
    public R<SysSerialMax> add(@ApiParam(value = "类型id", required = true) @PathVariable("typeId") int typeId) {
        SysSerialMax sysSerialMax = sysSerialMaxService.insertSysSerialMaxBySerialId(typeId);
        if(sysSerialMax == null){
            return R.fail("未查询到此编号！");
        }
        return R.ok(sysSerialMax);
    }

    /**
     * 删除编号最大值
     */
    @Log(title = "删除编号最大值", businessType = BusinessType.DELETE)
    @DeleteMapping("/delete/{maxIds}")
    @ApiOperation(value = "删除编号最大值")
    public AjaxResult remove(@ApiParam(value = "编号最大值id", required = true) @PathVariable Long[] maxIds) {
        return toAjax(sysSerialMaxService.deleteSysSerialMaxByMaxIds(maxIds));
    }
}
